package com.masterclient.app.businessmodule.minemodule.contract;

import com.eoner.baselib.container.CommonBaseResponse;
import com.eoner.baselib.presenter.FCBaseCallBack;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselibrary.bean.firstpager.SignBean;
import com.eoner.baselibrary.bean.personal.MedalDetailBean;
import com.masterclient.app.businessmodule.minemodule.api.MineHttpService;
import com.masterclient.app.businessmodule.minemodule.index.tutor.InviteInfoBean;
import com.masterclient.app.httpservice.RetrofitUtil;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
* Created by TMVPHelper on 2017/07/12
*/
public class PersonalPresenter extends PersonalContract.Presenter{


    @Override
    public void getSign() {

        mRxManager.add(toNewSubscribe(RetrofitUtil.getInstance().signInfo(), new FCBaseCallBack<CommonBaseResponse<SignBean>>() {
            @Override
            public void onSuccess(CommonBaseResponse<SignBean> bean) {
                mView.signData(bean.getData());
            }
        }));
    }


    @Override
    public void requestMedalDetail(String medalId) {
        Observable<MedalDetailBean> observable = MineHttpService.getInstance().medalInfoGet(medalId);
        Disposable disposable = toSubscribe(observable, new Consumer<MedalDetailBean>() {
            @Override
            public void accept(@NonNull MedalDetailBean bean) throws Exception {
                if(bean!=null&&bean.data!=null){
                    if (bean.getCode().equals("0")) {
                        mView.getMedalDetail(bean);
                    } else {
                        ToastUtil.showToast(bean.getMsg()!=null?bean.getMsg():SERVER_FAILED_STR);
                    }
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
                ToastUtil.showToast(WEB_FAILED_STR);
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void getShareInfo() {
        Observable<CommonBaseResponse<InviteInfoBean>> observable = MineHttpService.getInstance().getShareInfo();
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseResponse<InviteInfoBean>>() {
            @Override
            public void accept(@NonNull CommonBaseResponse<InviteInfoBean> bean) throws Exception {
                if(bean!=null&&bean.data!=null){
                    if (bean.code == 0) {
                        mView.getShareInfoSuccess(bean.data);
                    } else {
                        ToastUtil.showToast(bean.getMsg()!=null?bean.getMsg():SERVER_FAILED_STR);
                    }
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
                ToastUtil.showToast(WEB_FAILED_STR);
            }
        });
        mRxManager.add(disposable);
    }
}